﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace SocketServer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            EnableStartingWidgets();
            this.logger = new Logger(this, ServerStatus, DataLog);

        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            DisableStartingWidgets();
            StartServer();
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            EnableStartingWidgets();
            StopServer();
        }

        private void StartServer()
        {
            if (this.server != null)
            {
                this.logger.ServerLog(String.Format("Server already running at port {0}\n", this.port));
                return;
            }

            if (LoopbackCB.IsChecked == true)
            {
                this.server = new LoopbackServer(this.logger);
            }
            else
            {
                this.server = new Server(this.logger);
            }

            String portString = PortInput.Text;

            ServerStatus.Clear();
            this.port = 4444;

            try
            {
                this.port = Convert.ToInt32(portString);
            }
            catch
            {
                this.logger.ServerLog("Invalid port number!\n");
                this.logger.ServerLog("Starting server on default port 4444.\n");
                this.port = 4444;
            }

            this.server.Start(this.port);
        }

        private void StopServer()
        {
            this.logger.ServerLog("Stopping server...\n");
            this.server.Stop();
            this.server = null;
            this.port = 0;
        }


        private void DisableStartingWidgets()
        {
            Start.IsEnabled = false;
            Stop.IsEnabled = true;
            PortInput.IsEnabled = false;
            LoopbackCB.IsEnabled = false;
        }

        private void EnableStartingWidgets()
        {
            Start.IsEnabled = true;
            Stop.IsEnabled = false;
            PortInput.IsEnabled = true;
            LoopbackCB.IsEnabled = true;
        }

        private int port;
        private Logger logger;
        private Server server;

        private void ClearLog_Click(object sender, RoutedEventArgs e)
        {
            DataLog.Clear();
        }
    }
}
